#include <stdio.h>
#define MAXSIZE 16
char thousands[MAXSIZE][MAXSIZE]; // 2^32 = 4294967296 -> 4,294,967,296
int count = 0;

char * format_thousands_separator(unsigned long n){
	char ch;
	char *p,*q;
	int k,m;
	if(count > MAXSIZE - 1){
		fprintf(stderr,"Exceed %d call for number %d \n",MAXSIZE,n);
		return NULL;
	}
	p = q = &thousands[count][0];
	m = 0;
	do{
		k = n % 10;
		n = n / 10;
		ch = '0' + k;
		*p = ch;
		++p;
		++m;
		if(n && m % 3 == 0){
			*p = ',';
			++p;
		}		
	}while(n);
	*p = '\0';
	
	for(--p;q < p; ++q,--p){
		*q = *q ^ *p;
		*p = *q ^ *p;
		*q = *q ^ *p;
	}	
	
	return &thousands[count++][0];
}

int main(int argc,char **argv){
	printf("num1(%s), num2(%s), num3(%s)\n",
	format_thousands_separator(0),
	format_thousands_separator(123456),
	format_thousands_separator(23456789));
	return 0;
}
